package com.kwlt.smc.http;

import java.io.File;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kwlt.smc.entity.HttpSmsEntity;
import com.kwlt.smc.util.ConUtils;
import com.kwlt.smc.util.Constants;
import com.kwlt.smc.util.SendHttpUtils;


public class Zb003Impl implements HttpSmsInter{

	public static Logger rptLog =  LoggerFactory.getLogger("zb003rpt");
	
	public static Logger mtLog =  LoggerFactory.getLogger("zb003mt");
	
	public static Logger moLog =  LoggerFactory.getLogger("zb003mo");
	/**
	 * 获取状态报告
	 */
	public void getHttpSmsRpt(HttpSmsEntity httpSmsEntity) {
		System.out.println(httpSmsEntity.getNumbers()+" rpt thread is run success!");
		
	}

	/**
	 * 组装http发送map
	 * @param phone 手机号
	 * @param msg 发送内容
	 * @param httpSmsEntity 用户名和密码实体类
	 * @param expaCode 扩展码
	 * @return
	 */
	public Map<String,String> getSendMap(String phone,String msg,HttpSmsEntity httpSmsEntity,String expaCode,String localMsgId){
		Map<String,String> map = new HashMap<String,String>();
		map.put("user", httpSmsEntity.getUserName());
		map.put("pwd", httpSmsEntity.getPassword());
		map.put("dest", phone);
		map.put("msg", msg);
		map.put("msgid", localMsgId);
		if(expaCode!=null && !"".equals(expaCode)){
			map.put("ext", expaCode);
		}
		return map;
	}
	/**
	 * 发送短信
	 */
	public void sendHttpSms(HttpSmsEntity httpSmsEntity) {
		System.out.println(httpSmsEntity.getNumbers()+ " mt thread is run success!");
		File mtFile = FileUtils.getFile(httpSmsEntity.getMtPath()+"1/");
		while(true){
			for(File file:mtFile.listFiles()){
				try {
					List<String> list = FileUtils.readLines(file, "GBK");
					if(list!=null && list.size()>0){
						String line6 = list.get(6);
						String line11 = list.get(11);
						String localMsgId = list.get(18);
						String msg = list.get(20);
						String accessNumber = "";
						String phone = "";
						if(line6.startsWith("106")){
							accessNumber = line6;
							phone = line11;
						}else{
							accessNumber = line11;
							phone =  line6;
						}
						//截取扩展码
						String expaCode = accessNumber.substring(httpSmsEntity.getAccessNumbers().length(),accessNumber.length());
						Map<String, String> sendMap = getSendMap(phone, msg, httpSmsEntity, expaCode,localMsgId);
						// 内部用UTF-8
						String request = SendHttpUtils.sendHttpPostRequest(httpSmsEntity.getSendUrl(), sendMap,"UTF-8");
						mtLog.info("send http sms:"+phone+"\t"+accessNumber+"\t"+localMsgId+"\t"+msg+"\t"+request);
						//如果返回大于0则代表提交成功
						/*//则将成功返回的消息ID和手机号作为文件名生成临时文件到临时目录,并且删除MT文件
						FileUtils.writeStringToFile(new File(httpSmsEntity.getTmpPath()+request+phone),localMsgId,"GBK");*/
						file.delete();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			ConUtils.threadSleep(1000);
		}
	}
	
	public String getLocalMsgId(File tmpFile){
		try {
			List<String> list = FileUtils.readLines(tmpFile, "GBK");
			if(list!=null && list.size()>0)
				return list.get(0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取mo
	 */
	public void getHttpSmsMo(HttpSmsEntity httpSmsEntity){
		System.out.println(httpSmsEntity.getNumbers()+ " mo thread is run success!");
	}

}
